const GAME_OBJECTS = [];

export class GameObject {

    constructor() {
        GAME_OBJECTS.push(this);
        //时间增量
        this.timedelta = 0;
        //是否已经调用
        this.has_called_start = false;
    }

    //只执行一次
    start() {

    }

    //每一帧执行一次，除了第一帧
    update() {

    }

    //删除之前执行
    on_destroy() {

    }

    destroy() {

        this.on_destroy();

        for(let i in GAME_OBJECTS){
            if(GAME_OBJECTS[i] === this){
                GAME_OBJECTS.splice(i);
                break;
            }
        }
    }

}

let last_timestamp;   //上一次执行的时刻
const step = timestamp => {

    //of遍历数值
    for(let obj of GAME_OBJECTS){
        if(! obj.has_called_start){
            obj.has_called_start = true;
            obj.start();
        } else {
            obj.timedelta = timestamp - last_timestamp;
            obj.update();
        }
    }

    last_timestamp = timestamp;
    requestAnimationFrame(step);
}

requestAnimationFrame(step);